package com.dys.controller;

import com.dys.entity.User;
import com.dys.listener.LoginSessionActivationListener;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: 疾风
 * @ClassName: LoginServlet
 * @Description: 登录的Servlet
 * @CreateDate: 2024/09/25
 * @Version: v1.0.0
 */
@WebServlet(urlPatterns = "/loginServlet.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new User(username, password);
        // 存入Session中
        HttpSession session = req.getSession();
        session.setAttribute("user",user);
        // 绑定监听器
        session.setAttribute("listener",new LoginSessionActivationListener());
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("登录成功!");
    }
}
